package cn.demoa.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditStuActivity extends AppCompatActivity {
    private EditText editName;
    private EditText editAge;
    private RadioButton radioBtn1;
    private RadioButton radioBtn2;
    private RadioButton radioBtn3;
    private RadioButton radioBtn4;
    private RadioButton radioBtn5;
    private Button btnSave;
    private String no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_stu);

        setTitle("编辑学生");
        findView();

        initViewData();

        setBtnSaveClickListener();

    }

    /*设置保存按钮的点击事件*/
    private void setBtnSaveClickListener() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editName.getText().toString();
                int age = Integer.parseInt(editAge.getText().toString());

                int imgId = R.drawable.dog1;

                if(radioBtn1.isChecked()){
                    imgId = R.drawable.dog1;
                }else if(radioBtn2.isChecked()){
                    imgId = R.drawable.dog2;
                }else if(radioBtn3.isChecked()){
                    imgId = R.drawable.dog3;
                }else if(radioBtn4.isChecked()){
                    imgId = R.drawable.dog4;
                }else if(radioBtn5.isChecked()){
                    imgId = R.drawable.dog5;
                }

                Intent intent = new Intent();
                intent.putExtra("NAME", name);
                intent.putExtra("AGE", age);
                intent.putExtra("IMG", imgId);
                intent.putExtra("NO", no);
                setResult(3001, intent);

                finish();
            }
        });
    }

    /*初始化ViewData*/
    private void initViewData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE",0);
        int imgId = intent.getIntExtra("IMGID",R.drawable.dog1);
        no = intent.getStringExtra("NO");

        editName.setText(name);
        editAge.setText(String.valueOf(age));

        switch (imgId){
            case R.drawable.dog1:
                radioBtn1.setChecked(true);
                break;
            case R.drawable.dog2:
                radioBtn2.setChecked(true);
                break;
            case R.drawable.dog3:
                radioBtn3.setChecked(true);
                break;
            case R.drawable.dog4:
                radioBtn4.setChecked(true);
                break;
            case R.drawable.dog5:
                radioBtn5.setChecked(true);
                break;
        }
    }

    /*绑定组件*/
    private void findView() {
        editName = findViewById(R.id.edit_Name);
        editAge = findViewById(R.id.edit_Age);

        radioBtn1 = findViewById(R.id.edit_radioBtn1);
        radioBtn2 = findViewById(R.id.edit_radioBtn2);
        radioBtn3 = findViewById(R.id.edit_radioBtn3);
        radioBtn4 = findViewById(R.id.edit_radioBtn4);
        radioBtn5 = findViewById(R.id.edit_radioBtn5);

        btnSave = findViewById(R.id.edit_btnSave);

    }
}
